package me.sapling.single;

/**
 * @author sapling
 */
public class Backspin {

	private static String src;
	private static String des;

	public static String doWork(String src) {
		Backspin.src = src;
		// resolutionA();
		resolutionB();
		return des;
	}

	/**
	 * 解决方案A:
	 * 另外申请空间,依次以字符为单位进行从尾至首的排列,
	 * 适用于内存空间充足且字符串较短的情况
	 */
	private static void resolutionA() {
		StringBuilder tmp = new StringBuilder("");
		char chars[] = src.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			tmp.append(chars[chars.length-i-1]);
		}
		des = tmp.toString();
	}

	/**
	 * 解决方案B:
	 * 利用异或的置换特性,在不费用较大空间的情况下进行交换
	 */
	private static void resolutionB() {
		char chars[] = src.toCharArray();
		for (int i = 0; i < chars.length / 2; i++) {
			chars[i] ^= chars[chars.length-i-1];
			chars[chars.length-i-1] ^= chars[i];
			chars[i] ^= chars[chars.length-i-1];
		}
		des = String.valueOf(chars);
	}

}
